#coding:utf-8
import ConfigParser
import os

class ConfigConfig(object):
    def __init__(self,pwd):
        self.pwd = pwd
        self.cf = ConfigParser.ConfigParser()
    #获取配置文件路径
    def getpwd(self,pwd):
        return os.listdir(pwd)
    #读配置文件
    def confget(self,pwd):
        filename = self.getpwd(pwd)
        if len(filename) == 0:
            return  u"配置文件不存在！"
        else:
            for j in filename:
        #        print j.split('.')[-1]
                if j.split('.')[-1] == "conf":
                  # cf = ConfigParser.ConfigParser()
                    self.cf.read(pwd+j)
                    secs = self.cf.sections()
                return secs

    #获取键
    def getKey(self,pwd):
        relust = []
        secs = self.confget(pwd)
        for i in secs:
            opts = self.cf.options(i)
            relust.append(opts)
        return relust

    #获取键值对
    def getValues(self,pwd):
        ret = {}
        secs = self.confget(pwd)
        for i in secs:
            item = self.cf.items(i)
            for j,k in item:
                ret[j] = k
        print ret

    #添加标签
    def addBiao(self,pwd,newsection):
        filename = self.getpwd(pwd)
        for j in filename:
            print j
            conf = pwd + j
            print conf
            self.newsection = newsection
            print self.newsection
            self.cf.add_section(self.newsection)
            self.cf.write(open(conf,"a"))
        return








#                opts = cf.options(secs)
#                for i in opts:
#                    print i

if __name__ == '__main__':
    pwd = "D:\ROOT\myClass\config/"
    newsection = "a_d"
    b = ConfigConfig(pwd)

    #print b.getpwd(pwd)
    #print b.confget(pwd)
    #print b.getKey(pwd)
    #print b.getValues(pwd)
    print b.addBiao(pwd,newsection)










'''
cf = ConfigParser.ConfigParser()
cf.read('test.conf')
#获取得是所有标签的名字
secs = cf.sections()
1
#返字符串类型
str_val = cf.get("sec_a", "a_key1")
#返回整型值
int_val = cf.getint("sec_a", "a_key2")
print type(str_val),str_val
print type(int_val),int_val
#更新某个值
cf.set("sec_b", "b_key3", "new-$r")
cf.write(open("test.conf", "w"))
#添加一个标签
cf.add_section('a_new_section')
cf.write(open("test.conf","w"))

print u"更新成功"
'''